package com.shxc.smarttrade.account.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.base.BaseActivity;
import com.shxc.smarttrade.currency.model.CurrencyModel;
import com.shxc.smarttrade.info.Contents;
import com.shxc.smarttrade.utils.analysis.JsonAnalysisModel;
import com.shxc.smarttrade.utils.analysis.JsonAnalysisUtils;
import com.shxc.smarttrade.utils.Loger;
import com.shxc.smarttrade.utils.MyOkHttpUtils;
import com.shxc.smarttrade.utils.ToastUtils;

import org.xchris.view.annotation.ContentView;
import org.xchris.view.annotation.Event;
import org.xchris.view.annotation.ViewInject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * 输入简介
 */
@ContentView(R.layout.activity_account_introduction)
public class AccountIntroductionActivity extends BaseActivity {

    @ViewInject(R.id.introduction_edittext)
    EditText introduction_edittext;
    @ViewInject(R.id.count_textview)
    TextView count_textview;

    private InputMethodManager imm;

    private String introduction;

    @Event({R.id.cancel_textview, R.id.submit_textview})
    private void onClickk(View view) {

        switch (view.getId()) {
            case R.id.cancel_textview:
                //关闭
                finish();
                break;
            case R.id.submit_textview:
                //提交
                introduction = introduction_edittext.getText().toString();
                editUserInfo();
                break;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        imm = (InputMethodManager) this
                .getSystemService(Context.INPUT_METHOD_SERVICE);

        introduction_edittext.setText(spImp.getIntroduction());

        count_textview.setText((100 - spImp.getIntroduction().length()) + "/100");

        introduction_edittext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() < 101) {
                    int t = 100 - s.length();
                    count_textview.setText(t + "/100");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    if (s.length() >= 101) {
                        s.delete(s.length() - 1, s.length());
                    } else {
                        int count = 100 - s.length();
                        count_textview.setText(count + "/100");
                    }
                } else {

                }
            }
        });
    }

    private void editUserInfo() {
        loadingDialog.show();
        Map<String, Object> map = new HashMap<>();
        map.put("userId", spImp.getUserId());
        map.put("nick", spImp.getNick());
        map.put("sex", spImp.getSex());
        map.put("birthday", spImp.getBirthday());
        map.put("headUrl", spImp.getHeadUrl());
        map.put("introduction", introduction);

        MyOkHttpUtils.getServiceDatas(map, Contents.ACCOUNT_EDITUSERINFO, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void success(String response, int d) {
                if (response != null) {
                    JsonAnalysisModel jam = JsonAnalysisUtils.jsonAnalysis(response, CurrencyModel.class);
                    if (jam.isSuccess()) {
                        CurrencyModel model = (CurrencyModel) jam.getData();
                        if (model.getErr_code().equals("0")) {
                            spImp.setIntroduction(introduction);
                            ToastUtils.showToast("修改成功");
                            finish();
                        } else {
                            Loger.e("code", model.getErr_code());
                            ToastUtils.showToast("网络错误，请重试");
                        }
                        finish();
                    } else {
                        ToastUtils.showToast(jam.getMsg());
                    }
                }
                loadingDialog.dismiss();
            }

            @Override
            public void faild(Call call, Exception e, int id) {
                loadingDialog.dismiss();
            }
        });
    }

    // 点击EditText以外的任何区域隐藏键盘
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (v != null) {
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
        return super.onTouchEvent(event);
    }
}
